<?php
namespace app\index\controller;

use app\index\model\BidModel;
use app\index\model\CompanyModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\FlowMenuModel;
use app\index\model\FlowTaskModel;
use app\index\model\LoginTokenModel;
use app\index\model\MenuModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\service\MenuService;
use app\index\service\UserService;
use app\index\service\XingService;
use think\Db;
use think\Request;

class Index extends Common
{

    public function getuserinfo()
    {
        $data['id'] = $this->userinfo['id'];
        $data['realname'] = $this->userinfo['realname'];
        $data['deptname'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
        $data['comname'] = $this->cominfo['name'];
        $data['shortname'] = $this->cominfo['shortname'];
        $data['avatar'] = $this->userinfo['avatar'];
        $data['tel'] = $this->userinfo['tel'];

        $data['token_roletype'] = $this->userinfo['token_roletype'];
        $data['token_roletype_name'] = '';
        $rolearr = UserRoleModel::field('type as value')->where('id','in',$this->userinfo['roleid'])->group('type')->select()->toArray();
        foreach ($rolearr as &$item){
            $item['name'] = (new UserService())->get_user_role_type_name($item['value']);
            if($item['value'] == $this->userinfo['token_roletype']){
                $data['token_roletype_name'] = $item['name'];
            }
        }
        $data['rolearr'] = $rolearr;

        $data['watermarktxt'] = '';
        if($this->cominfo['iswater']){
            $data['watermarktxt'] = "筑为智慧经营管理系统 " . $this->userinfo['realname'];
        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function gettodononums()
    {
        $data['nums'] = TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->count();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function cutuserrole(Request $request)
    {
        $roletype = $request->param('roletype', 0, 'int');

        $roleidArr = UserRoleModel::where('id','in',$this->userinfo['roleid'])->group('type')->column('type');
        if(!in_array($roletype, $roleidArr)){
            return outputJson(4000, '操作失败');
        }

        LoginTokenModel::where(['id'=>$this->userinfo['token_id']])->update(['roletype'=>$roletype]);
        return outputJson(2000,lang('code_2000'));
    }


    public function getuserlock(Request $request)
    {
        $pass = $request->param('pass','','string');
        if($this->userinfo['pass'] == md5(md5($pass))){
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(4000,lang('code_4000'));
    }


    public function getusermenu(Request $request)
    {
        $pid = $request->param('pid',0, 'int');
        $isquan = $request->param('isquan',0, 'int');
        $data = (new MenuService())->user_menu($pid, $isquan);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function getmenuzuijin(Request $request)
    {
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['created_user','=',$this->userinfo['id']];
        $data = FlowMenuModel::field('menuid')->where($where)->order('id DESC')->limit(4)->distinct('menuid')->select()->toArray();
        foreach ($data as &$item){
            $info = MenuModel::where(['id'=>$item['menuid']])->find();
            if($info){
                $item = $info;
            }
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function seausermenu(Request $request)
    {
        $param = $request->post();
        $where[] = ['status','=',1];
        $where[] = ['ispc','=',1];

        $whereName = [];
        if(isset($param['seastr']) && !empty($param['seastr'])){
            $whereName[] = ['name','like', '%'.$param['seastr'].'%'];
        }

        $whereRole = '';
        if($this->userinfo['issuper'] < 1){
            $userMenuStr = (new MenuService())->get_user_authmenu();
            $userMenuStr = empty($userMenuStr) ? '0' : $userMenuStr;
            $whereRole = "isrole = 0 or (isrole = 1 and id in (".$userMenuStr."))";
        }

        $data = MenuModel::where($where)->where($whereRole)->where(['pid'=>0])->order('sort ASC')->select()->toArray();
        foreach ($data as $k=>$v){
            if($param['seamenuid'] > 0){
                if($v['id'] != $param['seamenuid']){
                    unset($data[$k]);
                    continue;
                }
            }

            $children222 = MenuModel::where($where)->where($whereRole)->where(['pid'=>$v['id']])->order('sort ASC')->select()->toArray();
            foreach ($children222 as $kk=>$vv){
                $children333 = MenuModel::where($where)->where($whereRole)->where($whereName)->where(['pid'=>$vv['id']])->order('sort ASC')->select()->toArray();
                if(!$children333){
                    unset($children222[$kk]);
                }else{
                    $children222[$kk]['children'] = $children333;
                }
            }
            if(!$children222){
                unset($data[$k]);
            }else{
                $children222 = array_values($children222);
                $data[$k]['children'] = $children222;
            }
        }
        $data = array_values($data);
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function dianmenuzuijin(Request $request)
    {
        $param = $request->post();
        $param['comid'] = $this->userinfo['comid'];
        $param['created_user'] = $this->userinfo['id'];
        $param['created_at'] = date('Y-m-d H:i:s');
        unset($param['token']);
        Db::startTrans();
        try {
            $id = FlowMenuModel::insertGetId($param);
            Db::commit();
        } catch (\Exception $e) {
            return outputJson(4000,lang('code_4000'));
            Db::rollback();
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function getusermenumob(Request $request)
    {
        $data = (new MenuService())->user_menu_mob();
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function getuserworker(){
        $data = [];


        $bill = FlowTaskModel::alias('task')
            ->field('bill.*,task.created_at as dt')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid'],'bill.isturn'=>1])
            ->where(['bill.status'=>0,'task.uid'=>$this->userinfo['id'],'task.state'=>1,'task.deleted_user'=>0,])
            ->order('task.created_at DESC')
            ->group('bill.id')->count();
        if($bill > 0){
            $data['bill'] = ['title' => '待办提醒', 'mess' => '您有（'.$bill.'）个待办事项！', 'href' => 'flow_hand/index.php'];
        }

        $todo = TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->count();
        if($todo > 0){
            $data['todo'] = ['title' => '消息提醒', 'mess' => '您有（'.$todo.'）条未读消息！', 'href' => 'todo/index.php'];
        }


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function getuserworkermob(){
        $nums = TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->count();
        return outputJson(2000,lang('code_2000'),$nums);
    }



















}
